﻿@*******************************************************************************************************
//  ValueListItems.cshtml - Gbtc
//
//  Copyright © 2016, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
//  file except in compliance with the License. You may obtain a copy of the License at:
//
//      http://opensource.org/licenses/MIT
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  02/18/2016 - Ritchie Carroll
//       Generated original version of source code.
//
//*****************************************************************************************************@
@using System.Collections.Specialized
@using GSF.Web.Model
@using PQDashboard.Model
@model AppModel
@{
    DataContext dataContext = Model.DataContext;
    NameValueCollection parameters = Request.QueryString;
    int parentID = int.Parse(parameters["ID"] ?? "0");

    ViewBag.CustomTitle = "Edit Items in \"" + parameters["Name"] + "\" Value List";
    ViewBag.PageHasMenu = false;

    ViewBag.HeaderColumns = new[]
    {   //    { "Field", "Label", "Classes" }
        new[] { "Text", "Text", "text-left" },
        new[] { "Description", "Description", "text-left" },
        new[] { "SortOrder", "Sort&nbsp;Order", "text-center" },
    };

    ViewBag.BodyRows = BodyRows().ToString();
    ViewBag.AddNewEditDialog = AddNewEditDialog(dataContext).ToString();
}
@helper BodyRows()
{
    <td width="40%" class="text-left valign-middle" nowrap><button type="button" class="btn btn-link" data-bind="text: Text, click: $parent.viewPageRecord"></button></td>
    <td width="40%" class="text-left table-cell-hard-wrap"><div data-bind="text: $($element.parentElement.parentElement).truncateToWidth(Description, 1.5), attr: {title: Description}"></div></td>
    <td width="15%" class="text-center valign-middle" data-bind="text: SortOrder"></td>
    <td width="5%" class="text-center valign-middle" nowrap>
        <button type="button" class="btn btn-xs" data-bind="click: $parent.editPageRecord, enable: $parent.dataHubIsConnected"><span class="glyphicon glyphicon-pencil"></span></button>
        <button type="button" class="btn btn-xs" data-bind="click: $parent.removePageRecord, enable: $parent.dataHubIsConnected"><span class="glyphicon glyphicon-remove"></span></button>
    </td>
}
@helper AddNewEditDialog(DataContext dataContext)
{
    <div class="col-md-6">
        @Html.Raw(dataContext.AddInputField<ValueList>("Key"))
        @Html.Raw(dataContext.AddInputField<ValueList>("Text"))
        @Html.Raw(dataContext.AddInputField<ValueList>("AltText1"))
        @Html.Raw(dataContext.AddInputField<ValueList>("AltText2"))
        @Html.Raw(dataContext.AddInputField<ValueList>("Abbreviation"))
    </div>
    <div class="col-md-6">
        @Html.Raw(dataContext.AddInputField<ValueList>("Value"))
        @Html.Raw(dataContext.AddCheckBoxField<ValueList>("Flag"))
        @Html.Raw(dataContext.AddTextAreaField<ValueList>("Description", rows: 3))
        @Html.Raw(dataContext.AddInputField<ValueList>("SortOrder"))
        @Html.Raw(dataContext.AddCheckBoxField<ValueList>("IsDefault"))
        @Html.Raw(dataContext.AddCheckBoxField<ValueList>("Hidden"))
        @Html.Raw(dataContext.AddCheckBoxField<ValueList>("Enabled"))
    </div>
}
@Html.Partial("PagedViewModel")
@section Scripts {
    <script>
        "use strict";

        @Html.Raw(Model.RenderViewModelConfiguration<ValueList>(ViewBag, "SortOrder", parentID))

        $(viewModel).on("beforeSave", function(event, observableRecord) {
            // Initialize selected value list record
            console.log(@parentID);
            observableRecord.GroupID(@parentID);
        });

        $(function() {
            $("#addNewEditDialog").on("shown.bs.modal", function () {
                // On add/edit dialog shown, disable data-dismiss buttons on parent pop-up
                const dismissButtons = $("#editValueListItemDialog", window.parent.document).find("button[data-dismiss]");
                dismissButtons.prop("disabled", true);
                dismissButtons.invisible();
            });

            $("#addNewEditDialog").on("hidden.bs.modal", function () {
                // On add/edit dialog hidden, re-enable data-dismiss buttons on parent pop-up
                const dismissButtons = $("#editValueListItemDialog", window.parent.document).find("button[data-dismiss]");
                dismissButtons.visible();
                dismissButtons.prop("disabled", false);
            });
        });
    </script>
}